package code.jam.activity;

import java.util.List;
import my.andoird.util.r1.JamThread;
import code.jam.R;
import code.jam.core.JamBGMManager;
import code.jam.core.JamManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class JamActivitySelectPlayMode extends Activity 
{
	public static final String DEBUG_TAG = "JamActivitySelectPlayMode";
	private view v = null;
	private JamManager jam_manager = null;
	private JamBGMManager bgm_manager = null;
	
	private boolean is_first_touch = false;
	private boolean is_first_drawing = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		Log.d(DEBUG_TAG, "onCreate()");
		
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		initializeJamManager();
		
		this.v = new view(this);
		setContentView( this.v );
	}
	
	@Override
	protected void onStart() 
	{
		Log.d(DEBUG_TAG, "onStart()");
		super.onStart();
	}
	
	@Override
	protected void onResume() 
	{
		Log.d(DEBUG_TAG, "onResume()");
		v.selectNone();
		super.onResume();
	}
	
	@Override
	protected void onRestart() 
	{
		Log.d(DEBUG_TAG, "onRestart()");
		is_first_drawing = false;
		v.selectNone();
		
		if( jam_manager.isFinish() == true )
		{
			finish();
		}
		
		super.onRestart();
	}
	
	@Override
	protected void onStop() 
	{
		Log.d(DEBUG_TAG, "onStop()");
		super.onStop();
	}
	
	@Override
	protected void onDestroy() 
	{
		Log.d(DEBUG_TAG, "OnDestroy()");
		
		if (jam_manager != null)
		{
			jam_manager.drop();
			jam_manager = null;
		}
		
		super.onDestroy();
	}
	
	private void initializeJamManager()
	{
		jam_manager = JamManager.getInstance();
		if (jam_manager == null)
		{
			Log.e(DEBUG_TAG, "can't take instance of JAM Manager");
			finish();
		}
		else
		{
			jam_manager.setJamProgressBitmap(this);
			bgm_manager = jam_manager.getBGMMangerInstance();
			
			if (jam_manager.start() == false)
			{
				Log.e(DEBUG_TAG, "can't take instance of JAM Manager");
				
				jam_manager.setFinish( true );
				
				Intent i = new Intent();
				i.setClass(getBaseContext(), code.jam.activity.JamActivityNotConnectedInternet.class);
				startActivity(i);
			}
		}
	}
	


	private void downloadBGMFiles()
	{
		bgm_manager = jam_manager.getBGMMangerInstance();

		if (bgm_manager == null)
		{
			Log.e(DEBUG_TAG, "can't take a instance of JAM BGM Manager");
			finish();
		}
		else
		{
			if (bgm_manager.start(jam_manager.getJamConfigContext()) == false)
			{	
				Log.e(DEBUG_TAG, "can't take a instance of JAM BGM Manager");
				finish();
			}
		}
		
		return;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) 
	{
		if( is_first_touch == true)
			return super.onTouchEvent(event);
			
		float x = event.getX();
		float y = event.getY();
		
		Log.d(DEBUG_TAG, String.format("x : %f = y : %f", event.getX(), event.getY()));
		
		boolean singleplay =  ((x > 273)&&(x < 475))&&(( y > 113)&&( y < 173)); 
		
		boolean multiplay = ((x > 273)&&(x < 475))&&(( y > 174)&&( y < 223)); 
		
		boolean howtoplay = ((x > 273)&&(x < 460))&&(( y > 224)&&( y < 300));
		
		boolean information = ((x > 450)&&(x < 480))&&(( y > 301)&&( y < 320));
		
		if(event.getAction() == MotionEvent.ACTION_DOWN)
		{
			if(singleplay)
			{
				v.selectSinglePlay();
			}
			else if(multiplay)
			{
				v.selectMultiPlay();
			}
			else if(howtoplay)
			{
				v.selectHowToPlay();
			}
			else if(information)
			{
				v.selectInformation();
			}
		}
		
		if(event.getAction() == MotionEvent.ACTION_UP)
		{
			if(singleplay)
			{
				is_first_drawing = true;
				
				new Thread(new Runnable() 
				{
					public void run() 
					{
						downloadBGMFiles();
						
						Intent i = new Intent();
						i.setClass(getBaseContext(), code.jam.activity.JamActivitySelectMusic.class);
						startActivity(i);
					}
				}, "THREAD_START_SELECT_MUSIC").start();
				
				new Thread(new Runnable() 
				{
					public void run() 
					{
						while( bgm_manager.isLoaded() == false )
						{
							v.postInvalidate();
							JamThread.sleep(150);
						}
					}
				}, "THREAD_DRAW_LOADING").start();
			
			}
			else if(multiplay)
			{
				Intent i = new Intent();
				i.setClass( this, code.jam.activity.JamActivityNotAvailableFunction.class);
				startActivity( i);
			}
			else if(howtoplay)
			{
				Intent i = new Intent();
				i.setClass( this, code.jam.activity.JamActivityHowToPlay.class);
				startActivity( i);
			}
			else if(information)
			{
				Intent i = new Intent();
				i.setClass( this, code.jam.activity.JamActivityInformation.class);
				startActivity( i);
			}
		}

		return super.onTouchEvent(event);
	}
	
	class view extends ImageView
	{
		Context context = null;
		private int loop = 0;
		private List<Bitmap> list = JamManager.getInstance().loadJamProgressBitmap();
		
		public view(Context context) 
		{
			super(context);
			setImageResource(R.drawable.main_none);
		}
		public void selectInformation() 
		{
			setImageResource(R.drawable.main_info);
		}
		public void selectHowToPlay() 
		{
			setImageResource(R.drawable.main_howto);
		}
		public void selectSinglePlay()
		{
			setImageResource(R.drawable.main_single);
		}
		public void selectMultiPlay()
		{
			setImageResource(R.drawable.main_multi);
		}
		public void selectNone()
		{
			setImageResource(R.drawable.main_none);
		}
		
		@Override
		protected void onDraw(Canvas canvas) 
		{
			is_first_touch = false;
			
			super.onDraw(canvas);
			
			if( (list == null) || (bgm_manager == null) )
			{
				Log.d(DEBUG_TAG, "NULL");
				return;
			}
			
			if( (is_first_drawing == true ) &&(bgm_manager.isChecked() == false) && (bgm_manager.isLoaded() == false) && (list != null) )
			{
				is_first_touch = true;
				
				canvas.drawBitmap(list.get(loop++), null, new Rect(0, 0, 480, 320), null);

				Paint paint = new Paint();
				paint.setAntiAlias( true );
				paint.setARGB(255, 128, 160, 160);

				canvas.drawText("downloading Music files....", 300, 260, paint);

				if(loop ==7) loop=0;
			}
		}
	}
}
